package a66weding.com.jiehuntong.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by admin on 2016/7/6.
 */
public class SharesUtils {
    private Context ctx;
    private String name = "jiehuntong";
    private static SharesUtils sharesUtils;
    private SharesUtils(Context ctx){
        this.ctx = ctx;
    }

    /**
     * 单例模式;
     * @param context
     * @return
     */
    public static SharesUtils getInstance(Context context){
        if(sharesUtils==null){
            sharesUtils =  new SharesUtils(context);
        }
        return sharesUtils;

    }
    //保存字符串;
    public void writeXML(String key,String value){
        SharedPreferences share = ctx.getSharedPreferences(name,0);
        SharedPreferences.Editor edt = share.edit();
        edt.putString(key, value);
        edt.commit();
    }
    //保存 int 类型;
    public void writeXML(String key,int value){
        SharedPreferences share = ctx.getSharedPreferences(name,0);
        SharedPreferences.Editor edt = share.edit();
        edt.putInt(key, value);
        edt.commit();
    }

    public String readXML(String key){
        SharedPreferences share = ctx.getSharedPreferences(name,0);
        String result = share.getString(key, "");
        return result;
    }

    public void CleadXml(){
        SharedPreferences share = ctx.getSharedPreferences(name,0);
        SharedPreferences.Editor edt = share.edit();
        edt.putString("id", "");
        edt.clear();
        edt.commit();
    }
    //保存图片sharePrefer
    public void  saveXml_bitmap(String bitmapname,Bitmap bitmap){
        //第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        //第二步:利用Base64将字节数组输出流中的数据转换成字符串String
        byte[] byteArray=byteArrayOutputStream.toByteArray();
        String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
        //第三步:将String保持至SharedPreferences
        SharedPreferences sharedPreferences= ctx.getSharedPreferences(name,0);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(name, imageString);
        editor.commit();
        Toast.makeText(ctx, "保存图片成功", Toast.LENGTH_SHORT).show();
    }
    public Bitmap getXml_bitmap(String bitmapname){
        SharedPreferences sharedPreferences=ctx.getSharedPreferences(name, Context.MODE_PRIVATE);
        //第一步:取出字符串形式的Bitmap
        String imageString=sharedPreferences.getString(bitmapname, "");
        //第二步:利用Base64将字符串转换为ByteArrayInputStream
        byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
        //第三步:利用ByteArrayInputStream生成Bitmap
        Bitmap bitmap= BitmapFactory.decodeStream(byteArrayInputStream);
        Toast.makeText(ctx,"获得bitmap"+bitmap,Toast.LENGTH_SHORT).show();
        return bitmap;
    }
}
